﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;

namespace SensorNetworkUI
{
    public class UserCollection : System.Collections.Generic.IList<UserDataStructure>
    {
        SqlConnection conn = new SqlConnection(@"Data Source=CHIMBS-PC\CHIMBS;Initial Catalog=sensors;Integrated Security=True");

        List<UserDataStructure> _userlist = new List<UserDataStructure>();

        public UserDataStructure Lookup(String username)
        {


            string stmt = @"SELECT [userid]
      ,[username]
      ,[usertype]
      ,[password]
      ,[fname]
      ,[lname]
      ,[nickname]
  FROM [sensors].[dbo].[user] where username like @username

";
            SqlCommand comm = new SqlCommand(stmt, conn);
            comm.Parameters.AddWithValue("@username", username);
            UserDataStructure ret = new UserDataStructure(username);
            
            try
            {
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();
                if (!dr.Read())
                    throw new UserNotFoundException(username) ;   
                ret.Password = dr["password"].ToString();
                ret.Userid = int.Parse(dr["userid"].ToString());

            }
            catch
            {
                throw new UserNotFoundException(username);
            }
            finally
            {

                conn.Close();
            }
            return ret;

        }

        public int IndexOf(UserDataStructure item)
        {

            return _userlist.IndexOf(item);
        }

        public void Insert(int index, UserDataStructure item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            SqlGeography geog = new SqlGeography();
            SqlGeographyBuilder bui = new SqlGeographyBuilder();
            

            bui.SetSrid(4326);
            bui.BeginGeography(OpenGisGeographyType.Point);
            bui.BeginFigure(22.1, 88.0);
            bui.EndFigure();
            bui.EndGeography();

            geog = bui.ConstructedGeography;


            string stmt = "insert into sensor values('chi',@db)";
            SqlCommand comm = conn.CreateCommand(); 
            comm.CommandText = stmt;
            SqlParameter par = new SqlParameter("@db", geog);
            par.UdtTypeName = "Geography";
            comm.Parameters.Add(par);

            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
        }

        public UserDataStructure this[int index]
        {
            get
            {
                return _userlist[index];
            }
            set
            {
                _userlist[index] = value;
            }
        }

        public void Add(UserDataStructure item)
        {

            string stmt = @"INSERT INTO [sensors].[dbo].[user]
           ([username]
           ,[password]
           
           
           ,[nickname])
     VALUES
           (@username
           
           ,@password
           
           
           ,@nick)
";





            SqlParameter username = new SqlParameter("@username", item.Uname);
            SqlParameter pass = new SqlParameter("@password", item.Password);
            SqlParameter nick = new SqlParameter("@nick", item.Nickname);
            SqlCommand cmd = new SqlCommand(stmt);
            cmd.Parameters.Add(username);
            cmd.Parameters.Add(pass);
            cmd.Parameters.Add(nick);
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();




            _userlist.Add(item);
            //throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(UserDataStructure item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(UserDataStructure[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return _userlist.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(UserDataStructure item)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<UserDataStructure> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}